package com.example.webviewtest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;

import com.example.utils.ControlUtils;
import com.example.webviewtest.databinding.ActivityTestA10Binding;
import com.example.webviewtest.databinding.ActivityTestA18Binding;
import com.pinyinsearch.model.PinyinSearchUnit;
import com.pinyinsearch.util.PinyinUtil;
import com.pinyinsearch.util.T9Util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.lang.reflect.Method;

public class TestA10Activity extends AppCompatActivity implements View.OnClickListener {
    ActivityTestA10Binding binding;

    private boolean mIsStart = true;
    private Toast mToast;
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
//            showToast(getCurrent());
            if (mIsStart) {
                // 因为Toast.LENGTH_SHORT的默认值是2000
                mHandler.sendEmptyMessageDelayed(0, 5000);
            }
        };
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeMessages(0);
        mIsStart = false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityTestA10Binding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mHandler.sendEmptyMessage(0);

        binding.button1.setOnClickListener(this);
        binding.button2.setOnClickListener(this);
        binding.button3.setOnClickListener(this);
        binding.button4.setOnClickListener(this);
        binding.button5.setOnClickListener(this);
        binding.button6.setOnClickListener(this);
        binding.button7.setOnClickListener(this);
        binding.button8.setOnClickListener(this);
        binding.button9.setOnClickListener(this);
        binding.button10.setOnClickListener(this);
        binding.button11.setOnClickListener(this);
        binding.button12.setOnClickListener(this);
        binding.button13.setOnClickListener(this);
        binding.button14.setOnClickListener(this);
        binding.button15.setOnClickListener(this);
        binding.button16.setOnClickListener(this);
        binding.button17.setOnClickListener(this);
        binding.button18.setOnClickListener(this);
        binding.button19.setOnClickListener(this);
        binding.button20.setOnClickListener(this);
        binding.button21.setOnClickListener(this);
        binding.button22.setOnClickListener(this);
        binding.button23.setOnClickListener(this);
        binding.button24.setOnClickListener(this);
        binding.button25.setOnClickListener(this);
        binding.button26.setOnClickListener(this);
        binding.button27.setOnClickListener(this);
        binding.button28.setOnClickListener(this);
        binding.button29.setOnClickListener(this);
        binding.button30.setOnClickListener(this);
        binding.button31.setOnClickListener(this);
        binding.button32.setOnClickListener(this);
        binding.button33.setOnClickListener(this);
        binding.button34.setOnClickListener(this);
        binding.button35.setOnClickListener(this);
        binding.button36.setOnClickListener(this);
        binding.button37.setOnClickListener(this);
        binding.button38.setOnClickListener(this);
        binding.button39.setOnClickListener(this);
        binding.button40.setOnClickListener(this);
        binding.button41.setOnClickListener(this);
        binding.button42.setOnClickListener(this);
        binding.button43.setOnClickListener(this);
        binding.button44.setOnClickListener(this);
        binding.button45.setOnClickListener(this);
        binding.button46.setOnClickListener(this);
        binding.button47.setOnClickListener(this);
        binding.button48.setOnClickListener(this);
    }

    private void sendBrodCast (String msg,int type) {
        Intent demo_intent = new Intent(msg);	//修改”demo”
        demo_intent.putExtra("status",type);//	0禁用,1允许
        demo_intent.setComponent(new ComponentName("com.android.settings","com.android.settings.UpdateTimeReceiver"));
        sendBroadcast(demo_intent);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button1:
                Intent lock_intent = new Intent("com.yunlian.lock");
                sendBroadcast(lock_intent);
                break;
            case R.id.button2:
                Intent uninstall = new Intent("com.yunlian.uninstall");
                uninstall.putExtra("uninstall_package","com.example.webviewtest");
                uninstall.setComponent(new ComponentName("com.android.settings","com.android.settings.UpdateTimeReceiver"));
                sendBroadcast(uninstall);
                break;
            case R.id.button3:
                sendBrodCast("com.yunlian.change.camera",1);
                break;
            case R.id.button4:
                sendBrodCast("com.yunlian.change.camera",0);

                break;
            case R.id.button5:
                sendBrodCast("com.yunlian.change.sms",1);
                break;
            case R.id.button6:
                sendBrodCast("com.yunlian.change.sms",0);
                break;
            case R.id.button7:
                sendBrodCast("com.yunlian.change.call",1);
                break;
            case R.id.button8:
                sendBrodCast("com.yunlian.change.call",0);
                break;
            case R.id.button9:
                sendBrodCast("com.yunlian.change.ring",1);
                break;
            case R.id.button10:
                sendBrodCast("com.yunlian.change.ring",0);
                break;
            case R.id.button11:
                sendBrodCast("com.yunlian.change.back",1);
                break;
            case R.id.button12:
                sendBrodCast("com.yunlian.change.back",0);
                break;
            case R.id.button13:
                sendBrodCast("com.yunlian.change.home",1);
                break;
            case R.id.button14:
                sendBrodCast("com.yunlian.change.home",0);
                break;
            case R.id.button15:
                sendBrodCast("com.yunlian.change.recent",1);
                break;
            case R.id.button16:
                sendBrodCast("com.yunlian.change.recent",0);
                break;
            case R.id.button17:
                sendBrodCast("com.yunlian.change.setting",1);
                break;
            case R.id.button18:
                sendBrodCast("com.yunlian.change.setting",0);
                break;
            case R.id.button19:
                sendBrodCast("com.yunlian.change.wifi",1);
                break;
            case R.id.button20:
                sendBrodCast("com.yunlian.change.wifi",0);
                break;
            case R.id.button21:
                sendBrodCast("com.yunlian.change.data",1);
                break;
            case R.id.button22:
                sendBrodCast("com.yunlian.change.data",0);
                break;
            case R.id.button23:
                sendBrodCast("com.yunlian.change.sim",1);
                break;
            case R.id.button24:
                sendBrodCast("com.yunlian.change.sim",0);
                break;
            case R.id.button25:
                sendBrodCast("com.yunlian.change.vpn",1);
                break;
            case R.id.button26:
                sendBrodCast("com.yunlian.change.vpn",0);
                break;
            case R.id.button27:
                sendBrodCast("com.yunlian.change.bluetooth",1);
                break;
            case R.id.button28:
                sendBrodCast("com.yunlian.change.bluetooth",0);
                break;
            case R.id.button29:
                sendBrodCast("com.yunlian.change.nfc",1);
                break;
            case R.id.button30:
                sendBrodCast("com.yunlian.change.nfc",0);
                break;
            case R.id.button31:
                sendBrodCast("com.yunlian.change.location",1);
                break;
            case R.id.button32:
                sendBrodCast("com.yunlian.change.location",0);
                break;
            case R.id.button33:
                sendBrodCast("com.yunlian.change.scan",1);
                break;
            case R.id.button34:
                sendBrodCast("com.yunlian.change.scan",0);
                break;
            case R.id.button35:
                sendBrodCast("com.yunlian.change.date",1);
                break;
            case R.id.button36:
                sendBrodCast("com.yunlian.change.date",0);
                break;
            case R.id.button37:
                sendBrodCast("com.yunlian.change.reset",1);
                break;
            case R.id.button38:
                sendBrodCast("com.yunlian.change.reset",0);
                break;
            case R.id.button39:
                sendBrodCast("com.yunlian.change.file",1);
                break;
            case R.id.button40:
                sendBrodCast("com.yunlian.change.file",0);
                break;
            case R.id.button41:
                sendBrodCast("com.yunlian.change.adb",1);
                break;
            case R.id.button42:
                sendBrodCast("com.yunlian.change.adb",0);
                break;
            case R.id.button43:
                sendBrodCast("com.yunlian.change.tether",1);
                break;
            case R.id.button44:
                sendBrodCast("com.yunlian.change.tether",0);
                break;
            case R.id.button45:
                sendBrodCast("com.yunlian.change.bar",1);
                break;
            case R.id.button46:
                sendBrodCast("com.yunlian.change.bar",0);
                break;
            case R.id.button47:
                sendBrodCast("com.yunlian.change.install",1);
                break;
            case R.id.button48:
                sendBrodCast("com.yunlian.change.install",0);
                break;
        }
    }


    /**
     * 获取当前电流
     */
    private String getCurrent() {
        String result = "null";
        try {
            Class systemProperties = Class.forName("android.os.SystemProperties");
            Method get = systemProperties.getDeclaredMethod("get", String.class);
            String platName = (String) get.invoke(null, "ro.hardware");
            if (platName.startsWith("mt") || platName.startsWith("MT")) {
                String filePath = "/sys/class/power_supply/battery/device/FG_Battery_CurrentConsumption";
                // MTK平台该值不区分充放电，都为负数，要想实现充放电电流增加广播监听充电状态即可
                result = "当前电流为：" + Math.round(getMeanCurrentVal(filePath, 5, 0) / 10.0f) + "mA";
                result += ", 电压为：" + readFile("/sys/class/power_supply/battery/batt_vol", 0) + "mV";
            } else if (platName.startsWith("qcom")) {
                String filePath ="/sys/class/power_supply/battery/current_now";
                int current = Math.round(getMeanCurrentVal(filePath, 5, 0) / 10.0f);
                int voltage = readFile("/sys/class/power_supply/battery/voltage_now", 0) / 1000;
                // 高通平台该值小于0时电池处于放电状态，大于0时处于充电状态
                if (current < 0) {
                    result = "充电电流为：" + (-current) + "mA, 电压为：" + voltage + "mV";
                } else {
                    result = "放电电流为：" + current + "mA, 电压为：" + voltage + "mV";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取平均电流值
     * 获取 filePath 文件 totalCount 次数的平均值，每次采样间隔 intervalMs 时间
     */
    private float getMeanCurrentVal(String filePath, int totalCount, int intervalMs) {
        float meanVal = 0.0f;
        if (totalCount <= 0) {
            return 0.0f;
        }
        for (int i = 0; i < totalCount; i++) {
            try {
                float f = Float.valueOf(readFile(filePath, 0));
                meanVal += f / totalCount;
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (intervalMs <= 0) {
                continue;
            }
            try {
                Thread.sleep(intervalMs);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return meanVal;
    }

    private int readFile(String path, int defaultValue) {
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(
                    path));
            int i = Integer.parseInt(bufferedReader.readLine(), 10);
            bufferedReader.close();
            return i;
        } catch (Exception localException) {
        }
        return defaultValue;
    }

    private void showToast(String content) {
        if (mToast == null) {
            mToast = Toast.makeText(TestA10Activity.this, content, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(content);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }
}